<template>

    <div class="dashboard-container">
        <div id="container">
            <el-form :inline="true" style="margin-left: 15px;">
                <el-form-item label="名称"><el-input v-model="storehouse" placeholder="请输入仓库名"></el-input></el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="pageQuery()">查询</el-button>
                <el-button type="primary" icon="el-icon-close" @click="reset()">重置</el-button>
                <el-button type="primary" icon="el-icon-plus el-icon-plus" @click="formLabelAlign2 = true">新增</el-button>
            </el-form>
        </div>

        <el-table :data="tableData" border style="width: 100%; margin-left: 15px;">
            <el-table-column prop="id" label="ID" width="50">
            </el-table-column>
            <el-table-column prop="storehouse" label="仓库名" width="180">
            </el-table-column>
            <el-table-column prop="remark" label="备注">
            </el-table-column>
            <el-table-column align="center" label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" icon=" el-icon-edit" @click="formLabelAlign3=true,sel(scope.row.id)">编辑</el-button>
                    <el-button type="primary" size="small" icon=" el-icon-delete" @click="del(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination align="center" @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="page" :background="background" :page-sizes="[5, 10, 15, 20]" :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
        </el-pagination>

        <!-- 点击新增按钮弹出界面 -->
        <div class="background-color">
            <el-dialog title="仓库添加" :visible.sync="formLabelAlign2">
                <el-form :model="storage" >
                    <el-form-item label="仓库" :label-width="formLabelWidth2"  >
                        <el-input v-model="storage.storehouse" class="size"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" :label-width="formLabelWidth2">
                        <el-input v-model="storage.remark" class="size"></el-input>

                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="formLabelAlign2 = false">取 消</el-button>
                    <el-button type="primary" @click="add(),submitForm('ruleForm')">确 定</el-button>
                </div>
            </el-dialog>
        </div>

         <div class="background-color">
            <el-dialog title="修改" :visible.sync="formLabelAlign3">
                <el-form :model="storage">
                    <el-form-item label="仓库" :label-width="formLabelAlign3">
                        <!-- <el-select v-model="storage.storehouse" aria-placeholder="请选择">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>   -->
                         <el-input v-model="storage.storehouse" class="size"></el-input>     
                         <!-- </el-select> -->
                    </el-form-item>
                    <el-form-item label="备注" :label-width="formLabelAlign3">
                       <el-input v-model="storage.remark" class="size"></el-input>  

                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="formLabelAlign3 = false">取 消</el-button>
                    <el-button type="primary" @click="upd()">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { page, getById, selectById, addStorage,update } from '@/api/Storage'
export default {
    data() {
        return {
            id: null,
            storehouse: null,
            remark: null,
            page: 1,
            pageSize: 10,
            tableData:[],
            storage:{
                id: null,
                storehouse: null,
                remark: null,
            },
            
            total: 0,
            dialogTableVisible: false,
            dialogFormVisible: false,
            formLabelAlign2: false,
            formLabelAlign3:false,
            formLabelWidth: '120px',
            
        }
    },
        
    
    //服务启动后自动执行分页查询
    created() {
        this.pageQuery();
    },

    methods: {
        //分页查询
        pageQuery() {
            const params = {
                id: this.id,
                name: this.name,
                remerk: this.remerk,
                page: this.page,
                pageSize: this.pageSize
            }
            page(params).then(res => {
                this.total = res.data.data.total
                this.tableData = res.data.data.records
            })
        },
        submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
        //重置
        reset() {
            this.name = ''
        },

        //添加
        add() {
            if (this.storage.storehouse == null) {
                this.$message.error("仓库名不能为空");
            } else {
            addStorage(this.storage).then(_res => {

                //修改成功
                this.$message.success("添加成功");

                this.$router.push('/StorageManage/StorageManage');

                this.formLabelAlign2 = false;

                //重新查询数据
                this.pageQuery();

                
            })
        }
        },

        //删除
        del(id) {
            this.$confirm(`此操作将永久删除ID=${id}的数据, 是否继续?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                getById(id)
                    .then(res => {
                        if (res.data.code === 1) {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            })
                        } else {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            })
                            this.pageQuery()
                        }
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },

        //查询
        sel(id){
            selectById(id).then(res=>{
                this.storage=res.data.data
            }) 
        },

        upd(){
        update(this.storage).then(res => {
          //修改成功
          this.$message.success("修改成功");
          
          this.formLabelAlign3=false;

          this.pageQuery();
          
          this.$router.push('/StorageManage/StorageManage');

         
      })
        }
    },

    handleSizeChange(val) {
        this.pageSize = val
        this.pageQuery()
    },
    handleCurrentChange(val) {
        this.page = val
        this.pageQuery()
    },
    // 监控多选框事件
    handleSelectionChange(selection) {
        this.selectNum = selection.length;
        this.selection = selection;
    },

}



</script>

<style scoped>
.size {
    height: 100%;
    width: 35%;
}
</style>